package com.taru.university.controller;

import com.taru.university.model.Photo;
import com.taru.university.service.PhotoService;
import com.taru.university.utils.Constant;
import com.taru.university.utils.ImageUtils;
import com.taru.university.utils.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@CrossOrigin
public class PhotoController {
    private static Logger logger = LoggerFactory.getLogger(PhotoController.class);

    @Autowired
    PhotoService photoService;

    @RequestMapping("/query/photo/articleId")
    @ResponseBody
    public Object queryPhotoById(String articleId){
        JsonResult result = null;
        try {
            Photo photo = photoService.queryPhotoById(articleId);
            logger.info("文章Id：{}", articleId);
            result = new JsonResult("查询成功", Constant.STATUC_SUCCESS,photo);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("查询异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/insert/photo")
    @ResponseBody
    public Object insertPhoto(Photo photo){
        JsonResult result = null;
        try {
            photoService.insertPhoto(photo);
            logger.info("添加成功");
            result = new JsonResult("添加成功", Constant.STATUC_SUCCESS);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("添加异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/delete/photo")
    @ResponseBody
    public Object deletePhoto(String photoId){
        JsonResult result = null;
        try {
            photoService.deletePhoto(photoId);
            logger.info("图片Id：{}", photoId);
            result = new JsonResult("删除成功", Constant.STATUC_SUCCESS);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("删除异常", Constant.STATUC_FAILURE);
        }
        return result;
    }


    /**
     * 通用上传
     * @param
     * @return
     */

    @RequestMapping("/common/upload")
    @ResponseBody
    public  Object   upload(@RequestParam("image-file") MultipartFile multipartFile){
        JsonResult<String> jsonResult=null;
        if(!multipartFile.isEmpty()){

            try {
                String  imageName= ImageUtils.save(multipartFile,"E:\\photo");
                jsonResult=new JsonResult<String>("上传成功",imageName);
                logger.info("已经成功上传图片:{}",imageName);
            } catch (Exception e) {
                logger.error("上传文件异常:{}",e.getMessage());
                e.printStackTrace();
                jsonResult=new JsonResult<String>("上传异常", Constant.STATUC_FAILURE);
            }

        }else{
            jsonResult=new JsonResult<String>("没有上传数据", Constant.STATUC_FAILURE);
        }

        return jsonResult;
    }
}
